﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections.Specialized;
using PBDesk.Utilities;

namespace PBDesk.Umbraco.Helper
{
    public static class CookieManager
    {


        public static void WriteCookie(string cookieName, string cookieValue, DateTime expire)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = new HttpCookie(cookieName);
                cookie.Value = cookieValue;
                cookie.Expires = expire;
                WriteCookie(cookie);
            }
        }
        public static void WriteCookie(string cookieName, string cookieValue)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = new HttpCookie(cookieName);
                cookie.Value = cookieValue;
                WriteCookie(cookie);
            }
        }

        public static void WriteCookie(string parentCookineName, string cookieName, string cookieValue)
        {
            if (IsCookieEnabled() && cookieName.IsNotNullAndNotEmpty())
            {
                WriteCookie(parentCookineName, new NameValueCollection() { { cookieName, cookieValue } });
            }
        }

        public static void WriteCookie(string parentCookineName, NameValueCollection keyValues)
        {
            if (IsCookieEnabled())
            {
                try
                {
                    HttpCookie parentCookie = GetCookie(parentCookineName);
                    if (parentCookie == null)
                        parentCookie = new HttpCookie(parentCookineName);

                    if (keyValues != null && keyValues.Count > 0)
                    {
                        for (int i = 0; i < keyValues.Count; i++)
                        {
                            parentCookie.Values[keyValues.GetKey(i)] = keyValues.Get(i);
                        }
                    }
                    WriteCookie(parentCookie);
                }
                catch (Exception ex)
                {
                    throw new Exception("Error at WriteCookie : " + ex.Message, ex);
                }

            }
        }
        public static void WriteCookie(HttpCookie cookie)
        {
            if (IsCookieEnabled())
            {
                if (cookie != null && cookie.Name.IsNotNullAndNotEmpty())
                {
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }

            }
        }

        public static string GetCookieValue(string parentCookieName, string childCookieName)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = GetCookie(parentCookieName);
                if (cookie != null && cookie.HasKeys)
                {
                    if (childCookieName.IsNotNullAndNotEmpty())
                    {
                        return cookie.Values[childCookieName];
                    }
                }
            }
            return string.Empty;
        }
        public static string GetCookieValue(string cookieName, bool checkForMultiValue = true)
        {
            if (IsCookieEnabled())
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
                if (cookie != null)
                {
                    if (cookie.HasKeys == false)
                    {
                        return cookie.Value;
                    }
                    else
                    {
                        if(checkForMultiValue == true)
                            throw new Exception("Cookie is MultiValued Cookie");
                        else
                            return cookie.Value;
                    }
                }
            }
            return string.Empty;

        }

        public static bool IsCookieEnabled()
        {
            if (HttpContext.Current != null)
            {
                if (HttpContext.Current.Request.Cookies != null)
                    return true;
            }
            return false;
        }

        public static HttpCookie GetCookie(string cookieName)
        {
            HttpCookie cookie = null;
            if (IsCookieEnabled() && cookieName.IsNotNullAndNotEmpty())
            {
                cookie = HttpContext.Current.Request.Cookies[cookieName];
            }
            return cookie;
        }
    }
}
